﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PrefabLightmapData : MonoBehaviour
{
	[System.Serializable]
	struct RendererInfo
	{
		public Renderer 	renderer;
		public int 			lightmapIndex;
		public Vector4 		lightmapOffsetScale;
	}

	[SerializeField]
	RendererInfo[]	m_RendererInfo;
	[SerializeField]
	Texture2D[] 	m_Lightmaps;

	void Awake ()
	{
		if (m_RendererInfo == null || m_RendererInfo.Length == 0)
			return;

        //var lightmaps = LightmapSettings.lightmaps;
        //UnityEngine.Debug.Log(name +  " lightmaps  " + lightmaps.Length);
        //var combinedLightmaps = new LightmapData[lightmaps.Length + m_Lightmaps.Length];

        //lightmaps.CopyTo(combinedLightmaps, 0);
        //for (int i = 0; i < m_Lightmaps.Length;i++)
        //{
        //    combinedLightmaps[i+lightmaps.Length] = new LightmapData();
        //    combinedLightmaps[i+lightmaps.Length].lightmapFar = m_Lightmaps[i];
        //}

		ApplyRendererInfo(m_RendererInfo);
        //UnityEngine.Debug.Log(name + " combinedLightmaps  " + combinedLightmaps.Length);
        //LightmapSettings.lightmaps = combinedLightmaps;
	}

	static void ApplyRendererInfo (RendererInfo[] infos)
	{
		for (int i=0;i<infos.Length;i++)
		{
			var info = infos[i];
            info.renderer.lightmapIndex = info.lightmapIndex;
                //+ lightmapOffsetIndex;
            UnityEngine.Debug.Log(" i " + i + " renderername " + info.renderer.gameObject.name + " info.renderer.lightmapIndex  " + info.renderer.lightmapIndex);
			info.renderer.lightmapScaleOffset = info.lightmapOffsetScale;
		}
	}

#if UNITY_EDITOR
	[UnityEditor.MenuItem("Assets/Bake Prefab Lightmaps")]
	static void GenerateLightmapInfo ()
	{
		if (UnityEditor.Lightmapping.giWorkflowMode != UnityEditor.Lightmapping.GIWorkflowMode.OnDemand)
		{
			Debug.LogError("ExtractLightmapData requires that you have baked you lightmaps and Auto mode is disabled.");
			return;
		}
		UnityEditor.Lightmapping.Bake();
        UnityEngine.Debug.Log(" LightmapSettings.lightmaps.Length " + LightmapSettings.lightmaps.Length);

		PrefabLightmapData[] prefabs = FindObjectsOfType<PrefabLightmapData>();

		foreach (var instance in prefabs)
		{
			var gameObject = instance.gameObject;
			var rendererInfos = new List<RendererInfo>();
			var lightmaps = new List<Texture2D>();
			
			GenerateLightmapInfo(gameObject, rendererInfos, lightmaps);
			
			instance.m_RendererInfo = rendererInfos.ToArray();
			instance.m_Lightmaps = lightmaps.ToArray();

			var targetPrefab = UnityEditor.PrefabUtility.GetPrefabParent(gameObject) as GameObject;
			if (targetPrefab != null)
			{
				//UnityEditor.Prefab
				UnityEditor.PrefabUtility.ReplacePrefab(gameObject, targetPrefab);
			}
		}
	}

	static void GenerateLightmapInfo (GameObject root, List<RendererInfo> rendererInfos, List<Texture2D> lightmaps)
	{
		var renderers = root.GetComponentsInChildren<MeshRenderer>();
		foreach (MeshRenderer renderer in renderers)
		{
			if (renderer.lightmapIndex != -1)
			{
				RendererInfo info = new RendererInfo();
				info.renderer = renderer;
				info.lightmapOffsetScale = renderer.lightmapScaleOffset;
                UnityEngine.Debug.Log(" renderer.lightmapIndex " + renderer.lightmapIndex + " renderer.lightmapScaleOffset " + renderer.lightmapScaleOffset);
                info.lightmapIndex = renderer.lightmapIndex;

                Texture2D lightmap = LightmapSettings.lightmaps[renderer.lightmapIndex].lightmapFar;
                if (lightmaps.IndexOf(lightmap) == -1)
                {
                    lightmaps.Add(lightmap);
                }
                

                //lightmaps.IndexOf(lightmap);
                //UnityEngine.Debug.Log(" info.lightmapIndex  " + info.lightmapIndex);
                //if (info.lightmapIndex == -1)
                //{
                //    info.lightmapIndex = lightmaps.Count;
                //    UnityEngine.Debug.Log(" => info.lightmapIndex  " + info.lightmapIndex);
                
                //}


				rendererInfos.Add(info);
			}
		}
	}
#endif

}